// 时间格式转换
export const timeQuantum = (time) => {
    // console.log(time);
    var date = new Date(time); //获取传入时间 并将其转化为date型
    var nowTime = new Date(); //获取当前时间
    var timeDifference = nowTime.getTime() - date.getTime(); //时间差的毫秒数
    // console.log("timeDifference" + timeDifference);
    let Y = date.getFullYear() + "-";
    let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    let h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
    let m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());
    let s = ":" + date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    // console.log(Y + M + D + h + m + s);
    
    let dh = date.getHours()
    let nh = nowTime.getHours()
    let dY = nowTime.getFullYear() + "-"

    if (timeDifference < 0) {
        return Y + M + D + h + m;
    } else {
        var days = Math.floor(timeDifference / (24 * 3600 * 1000)); //计算出相差天数
        // console.log("days" + days);
        if (days >= 2) {
            if (Y == dY) {
                return M + D + h + m;
            } else {
                return Y + M + D + h + m;
            }
        }
        if (days == 1 && (dh - nh) > 0) {
            return Y + M + D + h + m;
        }
        if (days == 1 && (dh - nh) <= 0) {
            return "昨天 " + h + m;
        }
        var leave1 = timeDifference % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000)); //计算出小时数
        // console.log("hours" + hours);
        if (hours > 3 && (dh - nh) > 0) {
            return "昨天 " + h + m;
        }
        if (hours > 3 && (dh - nh) <= 0) {
            return "今天 " + h + m;
        }
        if (hours > 1) {
            return hours + "小时前";
        }
        var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
        // console.log("minutes" + minutes);
        if (minutes > 3) {
            return minutes + "分钟前";
        }
        var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000); //计算相差秒数
        // console.log("seconds" + seconds);

        return "刚刚";
    }
}